using System;
using System.Collections;
using System.Collections.ObjectModel;

using EMan.Common.EnterpriseStructure;
using EMan.Interface.EnterpriseStructure;

using TrueCode.Core.Aspects;

namespace EMan.BL.EnterpriseStructure
{
  public class TreeNode : IStructureTreeNode
  {
    [NotNull] private readonly ObservableCollection< TreeNode > _children;
    [NotNull] private readonly DepartmentEditable _departmentEditable;
    private readonly TreeNode _parentNode;

    public TreeNode( TreeNode parentNode, [NotNull] Department department, ObservableCollection< TreeNode > children )
      : this( parentNode, department, children, false ){}

    public TreeNode( TreeNode parentNode, [NotNull] Department department, [NotNull] ObservableCollection< TreeNode > children, bool isNew )
    {
      if( department == null ) throw new ArgumentNullException( "department" );
      if( children == null ) throw new ArgumentNullException( "children" );

      _parentNode = parentNode;
      _departmentEditable = new DepartmentEditable( department, isNew );
      _children = children;

//      if( _department.Departments != null )
//        foreach( Department subdepartment in _department.Departments )
//          _children.Add( new TreeNode( this, subdepartment ) );

//      if( _department.Employees != null )
//        foreach( var employee in _department.Employees )
//          _children.Add( employee );
    }

    public TreeNode ParentNode
    {
      get { return _parentNode; }
    }

    [NotNull]
    public DepartmentEditable DepartmentEditable
    {
      get { return _departmentEditable; }
    }

    [NotNull]
    public ObservableCollection< TreeNode > Children
    {
      get { return _children; }
    }

    #region Implementation of IStructureTreeNode
    public IStructureTreeNodeValue Value
    {
      get { return _departmentEditable; }
    }

    IEnumerable IStructureTreeNode.Children
    {
      get { return Children; }
    }
    #endregion
  }
}